<#include "/main.ftl"/>
<body>
<@diamondNav/>
<div id="content" style="margin-left: 13%">
    <div class="panel-body" style="padding-bottom:0px;">
        <div class="panel panel-default">
            <div class="panel-heading">查询条件</div>
            <div class="panel-body" style="padding:2px">
                <form id="formSearch" class="form-horizontal">
                    <div class="" style="margin-top:15px">
                        <label class="control-label col-sm-1" for="interfaceType">接口类型</label>
                        <div class="col-sm-3">
                            <select id="interfaceType" class="form-control">
                                <option value="http">http</option>
                                <option value="webService">webService</option>
                                <option value="restful">restful</option>
                                <option value="dubbo">dubbo</option>
                            </select>
                        </div>
                        <div class="" style="margin-top:15px">
                            <label class="control-label col-sm-1" for="interfaceAliasName">接口别名</label>
                            <div class="col-sm-3">
                                <input type="text" class="form-control" id="interfaceAliasName">
                            </div>

                            <div class="col-sm-4" style="text-align:left;">
                                <button type="button" style="margin-left:50px" id="btn_query" class="btn  btn-success btnFont">
                                    查询
                                </button>
                            </div>
                        </div>
                </form>

            </div>
        </div>

        <div id="toolbar" class="btn-group">
            <button id="btn_add" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
            </button>
            <button id="btn_edit" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
            </button>
            <button id="btn_delete" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
            </button>
            <button id="btn_bindScript" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-wrench" aria-hidden="true"></span>维护自定义匹配脚本
            </button>
            <button id="btn_bindData" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-wrench" aria-hidden="true"></span>维护mock数据
            </button>
        </div>
        <table id="dataTable"></table>
    </div>
</div>
</body>
<script type="text/javascript">
    $(document).ready(function () {
        //1.初始化Table
        var oTable = new TableInit();
        oTable.Init();

        //2.初始化Button的点击事件
        var oButtonInit = new ButtonInit();
        oButtonInit.Init();

    });
    var meter;
    var winObj;
    var timeTrue=true;
    var scriptInterfaceMockId;
    /**
    * 开启监听脚本窗口,一旦窗口关闭,设置当前脚本关闭debug状态
    * */
    function myrefresh() {
        windowIdClosed();
        if(timeTrue) {
            //指定5秒刷新一次
            meter = setTimeout("myrefresh()", 1000);
        }
    }
    /**
     * 如果新开窗口关闭,取消定时器,关闭脚本debug
     * */
    function windowIdClosed() {
        if(winObj.closed) {
            clearTimeout(meter);
            timeTrue=false;
            scriptObj.setDebugStatus(scriptInterfaceMockId,false);
        }
    }

    var TableInit = function () {
        var oTableInit = new Object();
        //初始化Table
        oTableInit.Init = function () {
            $('#dataTable').bootstrapTable({
                url: '/interfaceMockController/getInterfaceMock',         //请求后台的URL（*）
                method: 'get',                      //请求方式（*）
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: true,                   //是否显示分页（*）
                sortable: false,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                queryParams: oTableInit.queryParams,//传递参数（*）,这里对应的是一个javascript的function方法
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 10,                       //每页的记录行数（*）
                pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                strictSearch: false,
                showColumns: false,                  //是否显示所有的列
                showRefresh: true,                  //是否显示刷新按钮
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                height: 550,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "id",        //每一行的唯一标识，一般为主键列
                showToggle: false,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: false,                   //是否显示父子表
                columns: [
                    {
                        checkbox: true
                    },
                    {
                        field: 'interfaceAliasName',
                        title: '接口别名'
                    }, {
                        field: 'interfaceName',
                        title: 'mock接口名称'
                    }, {
                        field: 'interfaceMethod',
                        title: 'mock方法名'
                    },
                    {
                        field: 'ip',
                        title: '调用者ip'
                    },
                    {
                        field: 'matchType',
                        title: 'mock匹配类型'
                    }
                    // },
                    // {
                    //     field: 'matchDataType',
                    //     title: '获取逻辑参数类型'
                    // }
                    ]
            });
        };

        //得到查询的参数
        oTableInit.queryParams = function (params) {
            var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                pageSize: params.limit,   //页面大小
                pageNo: params.offset,  //页码
                interfaceAliasName: $("#interfaceAliasName").val(),
                interfaceType: $("#interfaceType").val(),
                projectId: projectId
            };
            return temp;
        };
        return oTableInit;
    };


    var ButtonInit = function () {
        var oInit = new Object();
        oInit.Init = function () {
            $("#btn_add").click(function () {
                window.location.href = "${base}/interfaceMockController/editInterfaceMockPage?interfaceType=" + $("#interfaceType").val() + "&projectId=" + projectId;
            });

            $("#btn_edit").click(function () {
                var arrselections = $("#dataTable").bootstrapTable('getSelections');
                if (arrselections.length > 1) {
                    toastr.warning('只能选择一行进行编辑');
                    return;
                }
                if (arrselections.length <= 0) {
                    toastr.warning('请选择一条数据进行修改');
                    return;
                }
                window.location.href =
                        "${base}/interfaceMockController/editInterfaceMockPage?interfaceMockId=" + arrselections[0].interfaceMockId;
            });

            $("#btn_delete").click(function () {
                var arrselections = $("#dataTable").bootstrapTable('getSelections');
                if (arrselections.length <= 0) {
                    toastr.warning('请选择有效数据');
                    return;
                }

                Ewin.confirm({message: "确认要删除选择的数据吗？"}).on(function (e) {
                    if (!e) {
                        return;
                    }
                    $.ajax({
                        type: "post",
                        url: "${base}/interfaceMockController/deleteInterfaceMock",
                        data: {interfaceMockId: arrselections[0].interfaceMockId},
                        success: function (result) {
                            if (result.success) {
                                toastr.success('删除数据成功');
                                $("#dataTable").bootstrapTable('refresh');
                            } else {
                                toastr.error(result.msg);
                            }
                        },
                        error: function () {
                            toastr.error('Error');
                        },
                        complete: function () {

                        }

                    });
                });
            });
            $("#btn_bindScript").click(function () {
                var arrselections = $("#dataTable").bootstrapTable('getSelections');
                if (arrselections.length > 1) {
                    toastr.warning('只能选择一行进行编辑');
                    return;
                }
                if (arrselections.length <= 0) {
                    toastr.warning('请选择一条数据进行修改');
                    return;
                }
                var matchType =arrselections[0].matchType;
                if (matchType !="custom"){
                    toastr.warning('您不是自定义脚本,无法编辑脚本内容');
                    return;
                }
                var interfaceMockId =arrselections[0].interfaceMockId;
                var interfaceAliasName=arrselections[0].interfaceAliasName;
                var url ="${base}/scriptController/scriptPage?interfaceMockId="+interfaceMockId+"&interfaceAliasName="+interfaceAliasName;
                 winObj = window.open(url,"脚本编辑页面",
                        "height=800px,width=1500px,top=120px,left=200px"+
                        "toolbar=no,location=yes,directories=no,status=no,menubar=no, scrollbars=no, resizable=no");
                timeTrue=true;
                scriptInterfaceMockId=interfaceMockId;
                myrefresh();
            })

            $("#btn_bindData").click(function () {
                var arrselections = $("#dataTable").bootstrapTable('getSelections');
                if (arrselections.length <= 0) {
                    toastr.warning('请至少选择一条接口数据');
                    return;
                }
                if (arrselections.length > 1) {
                    toastr.warning('只能选择一行进行编辑');
                    return;
                }
                var interfaceMockId =arrselections[0].interfaceMockId;
                var matchType= arrselections[0].matchType;
                window.location.href="${base}/interfaceMockDataController/interfaceMockDataPage?interfaceMockId="+interfaceMockId+"&matchType="+matchType;
            });
            $("#btn_query").click(function () {
                $("#dataTable").bootstrapTable('refresh');
            });
        };

        return oInit;
    };
</script>